//
//  HPCheckBoxButton.m
//  ZZTong
//
//  Created by handpay on 13-5-13.
//  Copyright (c) 2013年 handpay. All rights reserved.
//

#import "HPCheckBoxButton.h"

@implementation HPCheckBoxButton

@synthesize label;
@synthesize icon;

@synthesize unCheckImage;
@synthesize checkedImage;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame withNormal:(NSString *)unCheck withChecked:(NSString *)check
{
    self = [super initWithFrame:frame];
    if (self) {
        self.unCheckImage = [UIImage imageNamed:unCheck];
        self.checkedImage = [UIImage imageNamed:check];
        
        UIImageView * iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.height, frame.size.height)];
        [iv setImage:self.checkedImage];
        checked = YES;
        self.icon = iv;
        [iv release];
        [self addSubview:self.icon];
    
        UILabel * tmpLabel = [[UILabel alloc]initWithFrame:CGRectMake(icon.frame.size.width + 6, 0, frame.size.width - icon.frame.size.width - 8, frame.size.height)];
        self.label = tmpLabel;
        [self.label setBackgroundColor:[UIColor clearColor]];
        self.label.textAlignment = NSTextAlignmentLeft;
        [tmpLabel release];
        
        [self addSubview:label];
        [self addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(BOOL)isChecked{
    return checked;
}

-(void)clicked{
//    checked = !checked;
    [self setChecked:!checked];
}

-(void)setChecked:(BOOL)flag{
    checked = flag;
    [icon setImage:checked ? self.checkedImage : self.unCheckImage];
    if (self.delegate) {
        [self.delegate checkBox:self changed:checked];
    }
}

- (void)dealloc{
    self.checkedImage = nil;
    self.unCheckImage = nil;
    self.delegate = nil;
    self.label = nil;
    self.icon = nil;
    
    [super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
